package com.hrm.controller;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hrm.dto.EmpDepDTO;
import com.hrm.dto.EmpTransferDTO;
import com.hrm.entity.Department;
import com.hrm.entity.Employee;
import com.hrm.entity.MyPage;
import com.hrm.entity.TransferRecord;
import com.hrm.service.DepartmentService;
import com.hrm.service.EmployeeService;
import com.hrm.service.TransferService;
import com.hrm.utils.JsonMsg;
import com.hrm.utils.Layui;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Auther: zy
 * @Date: 2020/5/30 09:31
 * @Description: 应聘登记,调动记录,员工档案等
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController{
    @Autowired
    EmployeeService employeeService;
    @Autowired
    DepartmentService departmentService;
    @Autowired
    TransferService transferService;

    /**
     * 跳转到 应聘登记页面
     * @return String
     */
    @RequestMapping("/unEmployee.html")
    public String unEmployee(){
        return "employeeRecord/unEmployeeList";
    }

    /**
     * 跳转调动记录页面
     * @return String
     */
    @RequestMapping("/tansferRecord.html")
    public String transferRecord(){
        return "employeeRecord/transferRecord";
    }

    /**
     * 跳转到员工档案页面
     * @return String
     */
    @RequestMapping("/employeeRecord.html")
    public String employeeRecord(){
        return "employeeRecord/employeeRecord";
    }

    /**
     * 跳转到编辑调动员工页面
     * @return String
     */
    @RequestMapping("/editTransfer.html")
    public String editTransferRecord(){
        return "employeeRecord/editTransfer";
    }

    /**
     * 跳转到应聘登记员工信息页面
     * @return
     */
    @RequestMapping("/editUnEmployee.html")
    public String editUnEmployee(){
        return "employeeRecord/editUnEmployee";
    }

    /**
     * 获取未录取员工List
     * @param myPage
     * @return ResponseEntity<Layui>
     */
    @RequestMapping("/unEmployeeList.html")
    @ResponseBody
    public ResponseEntity<Layui> unEmployeeList(MyPage myPage){
        PageHelper.startPage(myPage.getPageNum(),myPage.getPageSize());
        List<Employee> employeeList = employeeService.unEmployeeList();
        PageInfo pageInfo = new PageInfo(employeeList,5);
        Layui layui = Layui.data(pageInfo.getTotal(),pageInfo.getList());
        return ResponseEntity.ok(layui);
    }

    /**
     * 删除员工信息
     * @param employee
     * @return JsonMsg
     */
    @RequestMapping("/deleteEmp.html")
    @ResponseBody
    public JsonMsg deleteUnEmp(Employee employee){
        employeeService.deleteUnEmp(employee.getId());
        return JsonMsg.success();
    }


    /**
     * 更新未登记员工信息
     * @param empDepDTO
     * @return
     */
    @RequestMapping("/updateUnEmployee.html")
    @ResponseBody
    public JsonMsg updateUnEmployee(EmpDepDTO empDepDTO){
        Department department = departmentService.selectByName(empDepDTO.getDeptName());
        Employee employee = new Employee();
        employee.setId(empDepDTO.getId());
        employee.setDeptId(department.getDeptId());
        employee.setJobTitle(empDepDTO.getJobTitle());
        employee.setHireTime(empDepDTO.getHireTime());
        employeeService.updateEmp(employee);
        return JsonMsg.success();
    }


    /**
     * 更新员工调动记录
     * @param empTransferDTO
     * @return
     */
    @RequestMapping("/updateTransfer.html")
    @ResponseBody
    public JsonMsg updateTransfer(EmpTransferDTO empTransferDTO){
        Department department = departmentService.selectByName(empTransferDTO.getNewDepartment());
        Employee employee = new Employee();
        employee.setDeptId(department.getDeptId());
        employee.setJobTitle(empTransferDTO.getJobTitle());
        employee.setId(empTransferDTO.getId());
        employeeService.updateEmp(employee);
        TransferRecord transferRecord = new TransferRecord();
        transferRecord.setEmployeeId(empTransferDTO.getId());
        transferRecord.setOldDepartment(empTransferDTO.getOldDepartment());
        transferRecord.setNewDepartment(empTransferDTO.getNewDepartment());
        transferRecord.setTransferTime(empTransferDTO.getTransferTime());
        transferService.insertTransferRecord(transferRecord);
        return JsonMsg.success();
    }


}
